// pages/project/project.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showView: false,
    isHidden: true, //隐藏模态框
    showModal: false,
    arr:[ ],
    list:[],
    name:'',
    code:'',
    names:'',
    grade:'',
    year:'',
    batch:'',
    startTime:'',
    lastTime:'',
    type:'',
    state:'',
    protectionLevel:[],
    protectionLevelObj:[],
    batchObj:[],
    batchs:[],
    id:'', 
    Texts:'',
  },

    // 批次
    bindBatch: function(e) {
      // debugger
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        batch: this.data.batchs[e.detail.value]
      })
      console.log(this.data.batch)
    },
    // 级别
    bindProtectionLevel: function(e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        protection_level:this.data.protectionLevel[e.detail.value]
      })
      console.log(this.data.protection_level)
    },
    // 开始时间选择
    bindDateChange: function(e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        startTime: e.detail.value
      })
    },
    // 结束时间选择
    bindEnd: function(e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      // this.activity.endDate = e.mp.detail.value
      this.setData({
        // endDate : e.mp.detail.value
        lastTime: e.detail.value
      })
    },

    // 删除按钮
    goDelList: function (e) {
      let id = e.currentTarget.dataset.id;
      let that = this;
      wx.showModal({
        title: '提示',
        content: '您是否确定删除？',
        success: function (res) {
          if (res.confirm) {//这里是点击了确定以后
            wx.request({
              //接口地址
              url: app.BASE_SERVER_URL + '/api/wx/delManage', 
              method: 'POST',
              data: {
                id:id
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded', // 默认值
              },
              success :(res)=> { 
                if(res.data.code === "0") {
                    that.projectList();
                      wx.showToast({
                        title: '删除成功',              
                        icon: 'success',            
                        duration: 2000//持续的时间                
                      })
                  }         
              },
              fail:(err)=>{
                console.log(err)
              }
            })  
          } else {//这里是点击了取消以后
            return 
          }
        },
      })

    },   
    // 获取下标，点击显示隐藏
    toChange (e) {
      let that = this;
      let index = e.currentTarget.dataset.index;
      let arr =  that.data.arr
      for(let i = 0 ; i < arr.length; i++) {
        if (index == i) {
          arr[i] = !arr[i]
        } else {
          arr[i] = false
        }
      }
      that.setData({
        arr:arr,
      })
    },
    // 点击编辑，弹出模态框
    goProEdit:function(e){
      let id = e.currentTarget.dataset.id;
      wx.request({
        //接口地址
        url: app.BASE_SERVER_URL + '/api/wx/getManageDetailById', 
        method: 'POST',
        data: {
          id:id
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
        },
        success :(res)=> { 
          console.log(res.data)
          if(res.data.code === "0") {
            let param = res.data.data.bean;
            console.log(param)
            this.setData({
              names:param.name,
              code:param.code,
              batch:param.batch,
              protection_level:param.grade,
              year:param.year,
              type:param.type,
              state:param.state,
              startTime:param.startTime,
              lastTime:param.lastTime,
              id:param.id,
              Texts:'编辑',
            })   
          }    
          console.log(res.data)
          console.log(this.data.batch) 
          console.log(this.data.startTime) 
        },
        fail:(err)=>{
          console.log(err)
        }
      })
      this.showMd(e);
    },
  // 点击新增图片，显示隐藏内容
  onShowTitle:function(e){
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  // 点击添加,弹出模态框
  showMd: function(e) {
    //弹出框展示套餐的具体商品，根据data-group得到数组对象，然后再渲染到界面上
    var self = this;
    let groupList = e.currentTarget.dataset.group;
    // let groupList = e.target.dataset.group;
    //debugger
    this.setData({
      isHidden: false,//显示模态框，
      groupArr: groupList,
      showView: false,
      Texts:'添加',
    })
  },
  hiddenMd: function() {
    this.setData({
        isHidden: true//隐藏模态框
    })
  },

  // 点击快速定位，跳转页面
  goSmartSeek: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../project/SmartSeek/SmartSeek?type=1', 
    })
    this.setData({
      showView: false,
    })
  },
  // 点击快速巡检，跳转页面
  goQuickInspection: function (e) {
    //选择图片
    wx.chooseImage({
      count: 1, // 默认9，这里显示一次选择相册的图片数量 
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) { // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        let tempFiles = res.tempFiles
        //把选择的图片 添加到集合里
        for (let i in tempFiles) {
          tempFiles[i]['upload_percent'] = 0
          tempFiles[i]['path_server'] = ''
          if (tempFiles[i]['upload_percent'] == 0) {          
          //上传返回值
          const upload_task = wx.uploadFile({
            // 模拟https
            url: app.BASE_SERVER_URL + '/api/wx/fileUpload', //需要用HTTPS，同时在微信公众平台后台添加服务器地址  
            filePath: tempFiles[i]['path'], //上传的文件本地地址    
            name: 'file',
            formData: {
              'num': 1
            },
            //附近数据，这里为路径     
            success: function(res) {
              console.log(res.data);
              var data = JSON.parse(res.data);
              wx.navigateTo({
                url: '../project/QuickInspection/QuickInspection?type=1&pic='+data.data.url,
              })
              // this.setData({
              //   showView: false,
              // })
            }
          }
        )}
      }       
    }
  }) 
  },

  // 点击对象按钮跳转到对象页面
  goObject: function (e) {
    var that = this;
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../project/object/object?subjectId= ' + id +'&parentId=-1',
    })
    console.log(id)
  },
  // 点击指标按钮跳转到指标管理页面
  goTarget: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../project/target/target?mainId=' + id +'&userId=' + this.data.userId,
    })
  },
  // 点击属性，跳转到项目属性页面
  goProperties: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../project/Properties/Properties?subjectId=' + id,
    })
  },
  // 点击数据，跳转页面
  goData: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../project/ProData/ProData?subId= ' + id + '&type=1' ,
    })
  },

  // 点击设置图标，跳转到设置页面
  // goSetUp: function () {
  //   wx.navigateTo({
  //     url: '../setup/setup',
  //   })
  // },
  // 点击数据采集，跳转到数据采集的页面
  // goCollection: function () {
  //   wx.navigateTo({
  //     url: '../collection/collection',
  //   })
  // },


    // 搜索框
    searchInputAction: function (e) {
      this.setData({
        name:e.detail.value,
      })
    },
    // 弹窗
    showModal: function() {
        this.setData({
          showModal: true,
        })
      }, 
    //  弹出框蒙层截断touchmove事件    
    preventTouchMove: function () {
    },
    //  隐藏模态对话框
    hideModal: function () {
      this.setData({
        showModal: false
      });
    }, 
    //  对话框取消按钮点击事件   
    onCancel: function () {
      this.hideModal();
    }, 
    // 对话框确认按钮点击事件   
    onConfirm: function () {
      this.hiddenMd();
      this.setData({
        names:'',
        code:'',
        batch:'',
        protection_level:'',
        year:'',
        type:'',
        state:'',
        startTime:'',
        lastTime:'',
        id:'',
      })
    },

    // 项目列表接口
    projectList:function(){
      var that = this
      wx.request({
        url:app.BASE_SERVER_URL + '/api/wx/manageList', 
        method: 'POST',
        data: {
          userId:app.login_s.id,
          name:this.data.name
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
        },
        success :(res)=> { //需要注意的是，这里也可以是箭头函数，也有必须使用箭头函数的情况
          let arr = false;
          if(res.data.data.list && res.data.data.list.length) {
            arr = res.data.data.list.map(() => false)
          }        
          that.setData({
            arr : arr,
            list:res.data.data.list,
          })
          console.log(res.data)
          console.log(res.data.data.list);     
        },
        fail:(err)=>{
          console.log(err)
        }
      })
    },

    // 项目添加接口
    goConfirm:function(){
      // debugger
      var that = this
      console.log(this.data.names);     
      var batchObj = this.data.batchObj;
      for(var i =0;i<batchObj.length;i++){
        if(this.data.batch === batchObj[i].label){
          this.data.batch = batchObj[i].value;
        }
      }
      var protectionLevelObj = this.data.protectionLevelObj;
      for(var i =0;i<protectionLevelObj.length;i++){
        if(this.data.protection_level === protectionLevelObj[i].label){
          this.data. protection_level= protectionLevelObj[i].value;
        }
      }
      wx.request({
        url: app.BASE_SERVER_URL + '/api/wx/addManage', 
        method: 'POST',
        data:{
          id:this.data.id,
          code:this.data.code,
          name:this.data.names,
          grade:this.data.protection_level,
          year:this.data.year,
          type:this.data.type,
          state:this.data.state,
          batch:this.data.batch,
          startTime:this.data.startTime,
          lastTime:this.data.lastTime,
          userId:app.login_s.id,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
        },
        success :(res)=> { //需要注意的是，这里也可以是箭头函数，也有必须使用箭头函数的情况
          if (res.data.code === '0'){
            this.hiddenMd();
            // console.log(res.data);
            this.projectList();
            this.setData({
              names:'',
              code:'',
              batch:'',
              protection_level:'',
              year:'',
              type:'',
              state:'',
              startTime:'',
              lastTime:'',
              id:'',
            })
          }     
        },
        fail:(err)=>{
          console.log(err)
        }
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.projectList();
    wx.request({
      url: app.BASE_SERVER_URL + '/api/wx/getDictList',      //接口地址
      method: 'POST',
      data: {
        type:"protection_level",
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
      },
      success :(res)=> { //需要注意的是，这里也可以是箭头函数，也有必须使用箭头函数的情况
        console.log(res) ;
        var protectionLevelObj = res.data.body.data;
        var protectionLevel = [];
        for(var i = 0;i<protectionLevelObj.length;i++){
          protectionLevel.push(protectionLevelObj[i].label);
        }
        this.setData({
          protectionLevelObj:protectionLevelObj,
          protectionLevel:protectionLevel 
        })       
      },   
      fail:(err)=>{
        console.log(err)
      }
    })
    wx.request({
      url: app.BASE_SERVER_URL + '/api/wx/getDictList',      //接口地址
      method: 'POST',
      data: {
        type:"batch",
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
      },
      success :(res)=> { //需要注意的是，这里也可以是箭头函数，也有必须使用箭头函数的情况
        console.log(res) ;
        var batchObj = res.data.body.data;
        var batchs = [];
        for(var i = 0;i<batchObj.length;i++){
             batchs.push(batchObj[i].label);
        }
        this.setData({
          batchObj:batchObj,
          batchs:batchs 
        })                 
      },     
      fail:(err)=>{
        console.log(err)
      }
    }) 

  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userId:app.login_s.id
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})
